﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Po;
using nERP.Model;
using nERP.Model.Bas;

namespace nERP.ViewModel.Po.BuyDetailVMs
{
    public partial class BuyDetailListVM : BasePagedListVM<BuyDetail_View, BuyDetailSearcher>
    {

        protected override IEnumerable<IGridColumn<BuyDetail_View>> InitGridHeader()
        {
            return new List<GridColumn<BuyDetail_View>>{

                this.MakeGridHeader(x => x.BuyDetail_Seq).SetTitle(@Localizer["Page.序号"].Value),
                this.MakeGridHeader(x => x.BuyDetail_Buy).SetTitle(@Localizer["Page.采购"].Value),
                this.MakeGridHeader(x => x.BuyDetail_BuyDetailCode).SetTitle(@Localizer["Page.购货明细单号"].Value),
                this.MakeGridHeader(x => x.BuyDetail_Goods).SetTitle(@Localizer["Page.商品"].Value),
                this.MakeGridHeader(x => x.BuyDetail_GoodsName).SetTitle(@Localizer["Page.商品名称"].Value),
                this.MakeGridHeader(x => x.BuyDetail_GoodsSpec).SetTitle(@Localizer["Page.商品规格"].Value),
                this.MakeGridHeader(x => x.BuyDetail_GoodsUnit).SetTitle(@Localizer["Page.商品单位"].Value),
                this.MakeGridHeader(x => x.BuyDetail_BuyNumber).SetTitle(@Localizer["Page.数量"].Value),
                this.MakeGridHeader(x => x.BuyDetail_UnitPrice).SetTitle(@Localizer["Page.购货单价"].Value),
                this.MakeGridHeader(x => x.BuyDetail_DiscountPrice).SetTitle(@Localizer["Page.折扣价格"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TotalPrice).SetTitle(@Localizer["Page.金额"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TaxRate).SetTitle(@Localizer["Page.税率"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TaxUnitPrice).SetTitle(@Localizer["Page.含税单价"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TaxPrice).SetTitle(@Localizer["Page.税额"].Value),
                this.MakeGridHeader(x => x.BuyDetail_TotalTaxPrice).SetTitle(@Localizer["Page.含税金额"].Value),
                this.MakeGridHeader(x => x.BuyDetail_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.BuyDetail_Warehouse).SetTitle(@Localizer["Page.仓库"].Value),
                this.MakeGridHeader(x => x.BuyDetail_Status).SetTitle(@Localizer["Page.状态"].Value),
                this.MakeGridHeader(x => x.BuyDetail_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.BuyDetail_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.BuyDetail_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.BuyDetail_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<BuyDetail_View> GetSearchQuery()
        {
            var query = DC.Set<BuyDetail>()

                .CheckEqual(Searcher.Seq, x => x.Seq)
                .CheckEqual(Searcher.BuyId, x => x.BuyId)
                .CheckContain(Searcher.BuyDetailCode, x => x.BuyDetailCode)
                .CheckEqual(Searcher.GoodsId, x => x.GoodsId)
                .CheckContain(Searcher.GoodsName, x => x.GoodsName)
                .CheckContain(Searcher.GoodsSpec, x => x.GoodsSpec)
                .CheckContain(Searcher.GoodsUnit, x => x.GoodsUnit)
                .CheckEqual(Searcher.BuyNumber, x => x.BuyNumber)
                .CheckEqual(Searcher.UnitPrice, x => x.UnitPrice)
                .CheckEqual(Searcher.DiscountPrice, x => x.DiscountPrice)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckEqual(Searcher.TaxRate, x => x.TaxRate)
                .CheckEqual(Searcher.TaxUnitPrice, x => x.TaxUnitPrice)
                .CheckEqual(Searcher.TaxPrice, x => x.TaxPrice)
                .CheckEqual(Searcher.TotalTaxPrice, x => x.TotalTaxPrice)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckEqual(Searcher.WarehouseId, x => x.WarehouseId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new BuyDetail_View
                {
                    ID = x.ID,

                    BuyDetail_Seq = x.Seq,
                    BuyDetail_Buy = DC.Set<Buy>().Where(z0 => z0.BuyCode == x.BuyId).Select(y => y.BuyCode).FirstOrDefault(),
                    BuyDetail_BuyDetailCode = x.BuyDetailCode,
                    //BuyDetail_Goods = DC.Set<Goods>().Where(z0 => z0.GoodsCode == x.GoodsId).Select(y => y.GoodsName).FirstOrDefault(),
                    BuyDetail_Goods = x.GoodsId,
                    BuyDetail_GoodsName = x.GoodsName,
                    BuyDetail_GoodsSpec = x.GoodsSpec,
                    BuyDetail_GoodsUnit = x.GoodsUnit,
                    BuyDetail_BuyNumber = x.BuyNumber,
                    BuyDetail_UnitPrice = x.UnitPrice,
                    BuyDetail_DiscountPrice = x.DiscountPrice,
                    BuyDetail_TotalPrice = x.TotalPrice,
                    BuyDetail_TaxRate = x.TaxRate,
                    BuyDetail_TaxUnitPrice = x.TaxUnitPrice,
                    BuyDetail_TaxPrice = x.TaxPrice,
                    BuyDetail_TotalTaxPrice = x.TotalTaxPrice,
                    BuyDetail_Remark = x.Remark,
                    BuyDetail_Warehouse = DC.Set<Warehouse>().Where(z0 => z0.WarehouseCode == x.WarehouseId).Select(y => y.WarehouseName).FirstOrDefault(),
                    BuyDetail_Status = x.Status,
                    BuyDetail_CreateTime = x.CreateTime,
                    BuyDetail_UpdateTime = x.UpdateTime,
                    BuyDetail_CreateBy = x.CreateBy,
                    BuyDetail_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.BuyDetail_Buy).ThenBy(x => x.BuyDetail_Seq);
            return query;
        }


    }
    public class BuyDetail_View : BuyDetail
    {

        public int? BuyDetail_Seq { get; set; }
        public string BuyDetail_Buy { get; set; }
        public string BuyDetail_BuyDetailCode { get; set; }
        public string BuyDetail_Goods { get; set; }
        public string BuyDetail_GoodsName { get; set; }
        public string BuyDetail_GoodsSpec { get; set; }
        public string BuyDetail_GoodsUnit { get; set; }
        public decimal? BuyDetail_BuyNumber { get; set; }
        public decimal? BuyDetail_UnitPrice { get; set; }
        public decimal? BuyDetail_DiscountPrice { get; set; }
        public decimal? BuyDetail_TotalPrice { get; set; }
        public decimal? BuyDetail_TaxRate { get; set; }
        public decimal? BuyDetail_TaxUnitPrice { get; set; }
        public decimal? BuyDetail_TaxPrice { get; set; }
        public decimal? BuyDetail_TotalTaxPrice { get; set; }
        public string BuyDetail_Remark { get; set; }
        public string BuyDetail_Warehouse { get; set; }
        public dStatusTypeEnum? BuyDetail_Status { get; set; }
        public DateTime? BuyDetail_CreateTime { get; set; }
        public DateTime? BuyDetail_UpdateTime { get; set; }
        public string BuyDetail_CreateBy { get; set; }
        public string BuyDetail_UpdateBy { get; set; }

    }

}